package com.fram.service.api.v1.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.api.news.request.CommentSearchRequest;
import com.fram.bean.api.news.request.NewsSearchRequest;
import com.fram.bean.api.news.response.CommentSearchResponse;
import com.fram.bean.api.news.response.NewsDetailResponse;
import com.fram.bean.api.news.response.NewsSearchResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.canstant.PublicConstant;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.base.BaseService;
import com.fram.common.util.tencent.TencentUtils;
import com.fram.entity.NewsComment;
import com.fram.entity.NewsList;
import com.fram.entity.ResOperate;
import com.fram.entity.UserBrowsingRecords;
import com.fram.mapper.NewsCommentMapper;
import com.fram.mapper.NewsListMapper;
import com.fram.mapper.ResOperateMapper;
import com.fram.service.api.v1.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service("newsService")
public class NewsServiceImpl extends BaseService implements NewsService {

	@Autowired
	private NewsListMapper newsListMapper;

	@Autowired
	private NewsCommentMapper newsCommentMapper;

	@Autowired
	private ResOperateMapper operateMapper;

	@Autowired
	private TencentUtils tencentUtils;

	@Override
	public PageInfo<NewsSearchResponse> selectForApiList(PageBean<NewsSearchRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<NewsSearchResponse> list = newsListMapper.selectForApiList(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		for (NewsSearchResponse r : list) {
			r.setNewsIntroduce(PublicMethod.stripHtml(r.getNewsIntroduce()));
			if (r.getNewsIntroduce().length() > 60) {
				r.setNewsIntroduce(r.getNewsIntroduce().substring(0, 60) + "......");
			}
		}
		return new PageInfo<>(list);
	}

	@Override
	public NewsDetailResponse newsDetail(Long newsId, Long userId) throws ProException {
		NewsDetailResponse response = newsListMapper.detailForApi(newsId, userId);
		if (response == null) {
			throw new ProException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		// 加一个浏览量
		NewsDetailResponse update = new NewsDetailResponse();
		update.setId(response.getId());
		update.setViewNumber(response.getViewNumber() + 1);
		newsListMapper.updateByPrimaryKeySelective(update);

		// 记录浏览记录
		insertBrowsingRecords(new UserBrowsingRecords(response.getType(), response.getId(), "新闻公告"));
		return response;
	}

	@Override
	public PageInfo<CommentSearchResponse> selectCommentForApiList(PageBean<CommentSearchRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		request.getParam().setOperateZanResType(PublicConstant.RES_TYPE_5);
		List<CommentSearchResponse> list = newsCommentMapper.selectCommentForApiList(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void newsOperate(ResOperate operate) throws ProAffairException {
		ResOperate queryResOperate = operateMapper.select(operate);
		if (queryResOperate == null) {// 直接插入数据
			resAdditionAndSubtractionNumber(operate, 1);
			if (operateMapper.insertSelective(operate) == 0) {
				throw new ProRunTimeException(ErrCode.NEWS_OPERATE_FAIL.getReturnCode(), ErrCode.NEWS_OPERATE_FAIL.getReturnMsg());
			}
		} else {// 减掉数据
			resAdditionAndSubtractionNumber(operate, -1);
			if (operateMapper.deleteByPrimaryKey(queryResOperate.getId()) == 0) {
				throw new ProRunTimeException(ErrCode.NEWS_OPERATE_FAIL.getReturnCode(), ErrCode.NEWS_OPERATE_FAIL.getReturnMsg());
			}
		}
	}

	/** 资源操作加减数量 number为负数则减 **/
	public void resAdditionAndSubtractionNumber(ResOperate operate, int number) throws ProAffairException {
		if (PublicConstant.OPERATE_TYPE_2 == operate.getType() && (PublicConstant.RES_TYPE_3 == operate.getResType() || PublicConstant.RES_TYPE_4 == operate.getResType())) {// 在看+1
			NewsList news = newsListMapper.selectByPrimaryKey(operate.getResId());
			if (news == null) {
				throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), "操作数据不存在");
			}
			news.setLookNumber(news.getLookNumber() + number);
			if (newsListMapper.updateByPrimaryKeySelective(news) == 0) {
				throw new ProRunTimeException(ErrCode.NEWS_OPERATE_FAIL.getReturnCode(), ErrCode.NEWS_OPERATE_FAIL.getReturnMsg());
			}
		}
		if (PublicConstant.OPERATE_TYPE_1 == operate.getType() && PublicConstant.RES_TYPE_5 == operate.getResType()) {// 点赞+1
			NewsComment comment = newsCommentMapper.selectByPrimaryKey(operate.getResId());
			if (comment == null) {
				throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), "操作数据不存在");
			}
			comment.setZanNumber(comment.getZanNumber() + number);
			if (newsCommentMapper.updateByPrimaryKeySelective(comment) == 0) {
				throw new ProRunTimeException(ErrCode.NEWS_OPERATE_FAIL.getReturnCode(), ErrCode.NEWS_OPERATE_FAIL.getReturnMsg());
			}
		}
	}

	@Override
	@Transactional
	public NewsComment addComment(NewsComment request) throws ProAffairException {
		if (StringUtils.isEmpty(request.getContent())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		if (request.getContent().length() < 2 || request.getContent().length() > 200) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), "评论字数在2-200之间");
		}
		if (newsCommentMapper.insertSelective(request) == 0) {
			throw new ProRunTimeException(ErrCode.NEWS_COMMENT_FAIL.getReturnCode(), ErrCode.NEWS_COMMENT_FAIL.getReturnMsg());
		}
		if (request.getId() == null) {
			throw new ProRunTimeException(ErrCode.NEWS_COMMENT_FAIL.getReturnCode(), ErrCode.NEWS_COMMENT_FAIL.getReturnMsg());
		}
		if (!StringUtils.isEmpty(request.getContent())) {
			try {
				tencentUtils.checkMsg(request.getContent());
			} catch (ProException e) {
				throw new ProRunTimeException(e.getErrorCode(), e.getErrorMsg());
			}
		}
		CommentSearchRequest queryRequest = new CommentSearchRequest();
		queryRequest.setId(request.getId());
		queryRequest.setResId(request.getResId());
		queryRequest.setResType(request.getResType());
		List<CommentSearchResponse> list = newsCommentMapper.selectCommentForApiList(queryRequest);
		if (list == null || list.size() == 0 || list.size() != 1) {
			throw new ProRunTimeException(ErrCode.NEWS_COMMENT_FAIL.getReturnCode(), ErrCode.NEWS_COMMENT_FAIL.getReturnMsg());
		}
		return list.get(0);
	}
}
